﻿using SqlSugar;
using SqlSugar.IOC;
using System.Linq.Expressions;

namespace Shop.Core.Repository
{
    public class RepositoryBase<T> : SimpleClient<T> where T : class, new()
    {
        //protected SqlSugarScope Db;
        public RepositoryBase(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
        {
            base.Context = DbScoped.SugarScope;
#if DEBUG
            base.Context.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine("数据库查询：" + sql);
            };
#endif
        }

        public Task<bool> AnyAsync(Expression<Func<T, bool>> expression)
            => base.AsQueryable().AnyAsync(expression);

        public void BeginTran()
            => base.AsTenant().BeginTran();

        public void CommitTran()
            => base.AsTenant().CommitTran();
        public void RollbackTran()
            => base.AsTenant().RollbackTran();

        public ISugarQueryable<T1> Queryable<T1>() where T1 : class, new()
            => base.AsSugarClient().Queryable<T1>();
        public ISugarQueryable<T1, T2> Queryable<T1, T2>(Expression<Func<T1, T2, object[]>> joinExpression)
            => base.AsSugarClient().Queryable<T1, T2>(joinExpression);
        public ISugarQueryable<T1, T2, T3> Queryable<T1, T2, T3>(Expression<Func<T1, T2, T3, object[]>> joinExpression) 
            => base.AsSugarClient().Queryable<T1, T2, T3>(joinExpression);
        public ISugarQueryable<T1, T2, T3, T4> Queryable<T1, T2, T3, T4>(Expression<Func<T1, T2, T3, T4, object[]>> joinExpression) 
            => base.AsSugarClient().Queryable<T1, T2, T3, T4>(joinExpression);
        public ISugarQueryable<T1, T2, T3, T4, T5> Queryable<T1, T2, T3, T4, T5>(Expression<Func<T1, T2, T3, T4, T5, object[]>> joinExpression) 
            => base.AsSugarClient().Queryable<T1, T2, T3, T4, T5>(joinExpression);

        public ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
            => base.AsQueryable().Where(expression);

        public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, List<TReturn1>>> include1)
            => base.AsQueryable().Includes(include1);
        public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, TReturn1>> include1)
            => base.AsQueryable().Includes(include1);

        public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, List<TReturn1>>> include1, Expression<Func<TReturn1, List<TReturn2>>> include2)
            => base.AsQueryable().Includes(include1, include2);

        public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, List<TReturn1>>> include1, Expression<Func<TReturn1, TReturn2>> include2)
            => base.AsQueryable().Includes(include1, include2);

        public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, TReturn1>> include1, Expression<Func<TReturn1, TReturn2>> include2)
            => base.AsQueryable().Includes(include1, include2);

        public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, TReturn1>> include1, Expression<Func<TReturn1, List<TReturn2>>> include2)
           => base.AsQueryable().Includes(include1, include2);
    }
}
